<?php

class groupControlPage extends commonControlPage {

    public function groupModule() {
        $act = $_REQUEST['act'] ? $_REQUEST['act'] : 'list';
        call_user_func(array($this,$act.'Action'));
    }

    private function listAction() {
        if (POST && AJAX) {
        }

        $this->view->headlink = '<a href="?app='.APP.'&mod='.MOD.'&act=add">添加用户组</a>';

        $rows = $_ENV['db']->fetchAll("SELECT * FROM `admincp_member_group`");
        $this->view->groups = (array)$rows;
        $this->showMainPage('admincp/group_list');
    }

    private function addAction() {
        if (POST && AJAX) {
            $name = preg_replace('/\s*/','',$_POST['name']);
            $nodes = $_POST['nodes'];

            if (!$name || mb_strlen($name,'UTF-8') > 15) {
                $this->outputJson(0,'用户组名称应为1到15个字符之间');
            }

            if (empty($nodes)) {
                $this->outputJson(0,'请选择权限节点');
            }

            $_ENV['db']->runSql("INSERT INTO `admincp_member_group` ".$this->buildSql(array(
                    'name' => $name
                )));
            $gid = $_ENV['db']->insertId();
            if (!$gid) {
                $this->outputJson(0,'权限分配失败，数据库遇到未知错误');
            }

            foreach ((array)$nodes as $node_id) {
                $_ENV['db']->runSql("INSERT INTO `admincp_member_group_perm` ".$this->buildSql(array(
                        'gid' => $gid,
                        'node_id' => $node_id
                    )));
            }

            $this->outputJson(1,'操作成功');
        }
        $this->view->node_name = '添加用户组';

        $node_rowset = $_ENV['db']->fetchAll("SELECT n.*,m.name as menu_name,m.menu_id FROM `admincp_node` as n LEFT JOIN `admincp_menu` as m ON n.menu_group_id = m.menu_id");

        $nodes = array();
        foreach((array)$node_rowset as $node) {
            $nodes[$node['menu_name']][] = $node;
        }
        ksort($nodes);

        $this->view->nodes = $nodes;
        $this->showMainPage('admincp/group_add');
    }

    public function modiAction() {
        $gid = $_GET['gid'];
        $group = $_ENV['db']->fetch("SELECT * FROM `admincp_member_group` WHERE `gid` = '{$gid}'");
        if (!$group) $this->showMessage('指定的用户组不存在',AJXA?0:'back');

        if (POST && AJAX) 
		{
            $name = preg_replace('/\s*/','',$_POST['name']);
            $nodes = $_POST['nodes'];

            if (!$name || mb_strlen($name,'UTF-8') > 15) 
			{
                $this->outputJson(0,'用户组名称应为1到15个字符之间');
            }

            if (empty($nodes)) 
			{
                $this->outputJson(0,'请选择权限节点');
            }

			
			/*[更新组名]*/
            $_ENV['db']->runSql("UPDATE `admincp_member_group` ".$this->buildSql(array(
                    'name' => $name
                )).' WHERE `gid` = \''.$gid.'\'');

			
            $_ENV['db']->runSql("DELETE FROM `admincp_member_group_perm` WHERE `gid` = '{$gid}'");
			$users = $_ENV['db']->fetchAll("SELECT * FROM `admincp_member` WHERE `group_id` = '{$gid}'");
			$uids = '';
			foreach($users as $ukey=>$uvalue)
			{
				$uids.= $uvalue['uid'].',';
			}
			$uids=rtrim($uids,",");
			if($uids)
			{
				$_ENV['db']->runSql("DELETE FROM `admincp_member_perm` WHERE `uid` in ({$uids})");
				$uidarr = explode(',',$uids);
			}
			
		
			foreach ($nodes as $node_id) 
			{
                $_ENV['db']->runSql("INSERT INTO `admincp_member_group_perm` ".$this->buildSql(array(
                        'gid' => $gid,
                        'node_id' => $node_id
                    )));
					
					if(count($uidarr)>0)
					{
						foreach($uidarr as $uid)
						{
							$_ENV['db']->runSql("INSERT INTO `admincp_member_perm` ".$this->buildSql(array(
								'uid' => $uid,
								'node_id' => $node_id
							)));
						}	
					}
            }
            $this->outputJson(1,'操作成功');
        }

        $group_nodes = $_ENV['db']->fetchAll("SELECT * FROM `admincp_member_group_perm` WHERE `gid` = '{$_GET['gid']}'");
        $group_nodes_relation = array();
        foreach ($group_nodes as $group_node) {
            $group_nodes_relation[$group_node['node_id']] = $group_node['gid'];
        }

        $node_rowset = $_ENV['db']->fetchAll("SELECT n.*,m.name as menu_name,m.menu_id FROM `admincp_node` as n LEFT JOIN `admincp_menu` as m ON n.menu_group_id = m.menu_id");

        $nodes = array();
        foreach((array)$node_rowset as $node) {
            $nodes[$node['menu_name']][] = $node;
        }
        ksort($nodes);

        $this->view->nodes = $nodes;
        $this->view->gnr = $group_nodes_relation;
        $this->view->group = $group;
        $this->showMainPage('admincp/group_modi');
    }
}